package com.xinjian.jianba.utils;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import io.rong.RongCloud;
import io.rong.methods.user.User;
import io.rong.models.Result;
import io.rong.models.response.TokenResult;
import io.rong.models.user.UserModel;
@Component
public class RongYunUtil {
    /**
     * 融云appKey
     */
	private final static String env="a";//擦肩不过标识
	
    private static  String appKey;
    /**
     * 融云appSecret
     */
    private static  String appSecret;
    @Value("${RongYun.appKey}")
	public void setAppKey(String appKey) {
		RongYunUtil.appKey = appKey;
	}
    @Value("${RongYun.appSecret}")
	public  void setAppSecret(String appSecret) {
		RongYunUtil.appSecret = appSecret;
	}
	public static String getRongYunToken(String userId,String nickName,String headAdress) throws Exception{
	RongCloud rongCloud = RongCloud.getInstance(appKey, appSecret);
	User User = rongCloud.user;
	/**
     * API 文档: http://www.rongcloud.cn/docs/server_sdk_api/user/user.html#register
     *
     * 注册用户，生成用户在融云的唯一身份标识 Token
     */
    UserModel user = new UserModel()
            .setId(userId+env)
            .setName(nickName)
            .setPortrait(headAdress);
    TokenResult result = User.register(user);
    return result.toString();
    }
	public static Result updateRongYunToken(String userId,String nickName,String headAdress) throws Exception{
		RongCloud rongCloud = RongCloud.getInstance(appKey, appSecret);
		User User = rongCloud.user;
		UserModel user = new UserModel()
	            .setId(userId+env)
	            .setName(nickName)
	            .setPortrait(headAdress);
		Result result = User.update(user);
		return result;
	}
}
